package cn.com.homeyoda.aimate.aihealth.ui.my;

import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.AVATAR;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.LOGOUT_SUCCESS;
import static cn.com.homeyoda.aimate.aihealth.utils.AppConstants.NAME;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.wzq.mvvmsmart.utils.GlideLoadUtils;
import com.wzq.mvvmsmart.utils.SPUtils;

import java.util.List;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityMyBinding;
import cn.com.homeyoda.aimate.aihealth.ui.my.family.FamilyFragment;
import cn.com.homeyoda.aimate.aihealth.ui.my.feedback.FeedbackFragment;
import cn.com.homeyoda.aimate.aihealth.ui.my.questionnaire.QuestionnaireFragment;
import cn.com.homeyoda.aimate.aihealth.views.CenterDialog;
import cn.com.homeyoda.aimate.aihealth.views.NavigationButton;

public class MyActivity extends BaseActivity<ActivityMyBinding, MyViewModel> implements View.OnClickListener {

    private NavigationButton mCurrentNavButton;
    private FragmentManager mFragmentManager;

    @Override
    public MyViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(MyViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_my;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();

        binding.titleBar.ivBack.setOnClickListener(this);
        binding.titleBar.tvTitle.setText(getString(R.string.personal_center));

        String avatar = SPUtils.getInstance().getString(AVATAR);
        String name = SPUtils.getInstance().getString(NAME);
        if (!TextUtils.isEmpty(avatar)) {
            GlideLoadUtils.loadImg(binding.myContent.ivUserIcon, avatar, R.mipmap.ic_member1);
        }
        if (!TextUtils.isEmpty(name)) {
            binding.myContent.tvUserName.setText(name);
        }

        initNavBar();
    }

    private void initNavBar() {
        mFragmentManager = getSupportFragmentManager();
        binding.myContent.navItemWdjt.init(R.drawable.ic_my_wdjt, R.string.tab_wdjt, FamilyFragment.class);
        binding.myContent.navItemWdwj.init(R.drawable.ic_my_wdwj, R.string.tab_wdwj, QuestionnaireFragment.class);
        binding.myContent.navItemWdfk.init(R.drawable.ic_my_wdfk, R.string.tab_wdfk, FeedbackFragment.class);

        binding.myContent.navItemWdjt.setOnClickListener(this);
        binding.myContent.navItemWdwj.setOnClickListener(this);
        binding.myContent.navItemWdfk.setOnClickListener(this);

        binding.myContent.tvLogout.setOnClickListener(this);

        doSelect(binding.myContent.navItemWdjt);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.nav_item_wdjt) {
            doSelect(binding.myContent.navItemWdjt);
        } else if (id == R.id.nav_item_wdwj) {
            doSelect(binding.myContent.navItemWdwj);
        } else if (id == R.id.nav_item_wdfk) {
            doSelect(binding.myContent.navItemWdfk);
        } else if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_logout) {
            new CenterDialog(this, getString(R.string.logout_hint), () -> {
                viewModel.doLogout();
                doLogout();
                LiveEventBus.get(LOGOUT_SUCCESS).post(true);
                finish();
            }).show();
        }
    }

    private void doSelect(NavigationButton newNavButton) {

        NavigationButton oldNavButton = null;
        if (mCurrentNavButton != null) {
            oldNavButton = mCurrentNavButton;
            if (oldNavButton == newNavButton) {
                onReselect(oldNavButton);
                return;
            }
            oldNavButton.setSelected(false);
        }
        newNavButton.setSelected(true);
        doTabChanged(oldNavButton, newNavButton);
        mCurrentNavButton = newNavButton;
    }

    private void doTabChanged(NavigationButton oldNavButton, NavigationButton newNavButton) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavButton != null) {
            if (oldNavButton.getFragment() != null) {
                ft.detach(oldNavButton.getFragment());
            }
        }
        if (newNavButton != null) {
            Fragment fragment = Fragment.instantiate(this,
                    newNavButton.getClx().getName(), null);
            setFragment(fragment);
        }
        ft.commit();
    }

    public void setFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.flContainer, fragment)
                .commit();
    }

    public void onReselect(NavigationButton navigationButton) {
        Fragment fragment = navigationButton.getFragment();
        if (fragment != null) {
            fragment.onResume();// 是否要重新刷新？
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getSupportFragmentManager().getFragments();
        if (!getSupportFragmentManager().getFragments().isEmpty()) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            for (Fragment mFragment : fragments) {
                mFragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }
}